import os
import json
import nltk
import jieba

def init_app():
    """初始化应用程序环境"""
    # 创建必要的目录
    os.makedirs("data", exist_ok=True)
    os.makedirs("config", exist_ok=True)
    os.makedirs("resources", exist_ok=True)
    
    # 创建默认配置文件
    if not os.path.exists("config/model_config.json"):
        default_config = {
            "host": "http://localhost:11434",
            "model": "",
            "parameters": {
                "temperature": 0.7,
                "max_tokens": 4094,
                "top_p": 0.9,
                "context_length": 4096,
                "memory_size": 20,
                "frequency_penalty": 0.5,
                "presence_penalty": 0.5
            }
        }
        with open("config/model_config.json", "w", encoding='utf-8') as f:
            json.dump(default_config, f, ensure_ascii=False, indent=4)
    
    # 下载必要的 NLTK 数据
    try:
        nltk.data.find('tokenizers/punkt')
    except LookupError:
        nltk.download('punkt') 